/**
 * Pet编辑器
 */
/* jshint esversion: 6 */
(function () {
    // 加载依赖
    PetUI.SetJS("/lib/Editor/showdown/showdown.min.js");
    // * 代码高亮
    PetUI.SetJS("/lib/Editor/highlight/highlight.pack.js");
    PetUI.SetCSS("/lib/Editor/highlight/styles/vs.css");
    // * 公式编辑器
    PetUI.SetJS("/lib/Editor/katex/katex.min.js");
    PetUI.SetCSS("/lib/Editor/katex/katex.min.css");
    // * 样式
    PetUI.SetCSS("/lib/Editor/PetEditor.css");
    PetUI.SetCSS("/lib/Editor/Markdown.css");
    // 编辑器创建函数
    PetUI.Editor = function (vessel) {
        // 创建容器
        var div = vessel.Create("div");
        div.classList = "PetEditor";
        div.CSS('display:block;');
        // 创建编辑容器
        var text = div.Create("textarea");
        text.contentEditable = "true";
        text.classList = "Text";
        vessel.Text = text;
        // * 菜单
        let menu = PetUI.ContextNavbar();
        vessel.MenuUpload = menu.Submenu("提交");
        // * 显示菜单
        text.UI.Click = function (e) {
            if (e.button == 2) {
                menu.Display(true);
                menu.CSS({
                    top: e.y + "px",
                    left: e.x + "px",
                    position: "fixed"
                })
            }
        };
        div.Add(menu)
        // * 打开目标文件
        vessel.OnValueGet = function (url) {
            text.value = "";
            vessel.ValueFile = "";
            PetUI.HTTP.Get({
                url: url,
            }, function (error, data) {
                if (error != null) {
                    PetUI.PromptErr(error);
                } else {
                    // 处理显示
                    text.value = data;
                }
            });
            return vessel;
        };
        // * 拦截UI被隐藏
        Object.defineProperty(vessel, 'Display', {
            get: function () {
                return function (state) {
                    if (state == undefined) {
                        return this.style.display != 'none';
                    }
                    if (state) {
                        this.style.display = 'inline';
                    } else {
                        text.value = "";
                        vessel.ValueUR = "";
                        this.style.display = 'none';
                    }
                    return state;
                };
            }
        });
        // Textarea 高度自适应
        PetUI.Editor.HighlyAdaptive(text);
    };
    //  高度适应
    PetUI.Editor.HighlyAdaptive = function (Obj) {
        var setStyle = function (Obj) {
            Obj.style.height = 'auto';
            Obj.style.height = Obj.scrollHeight + 'px';
        };
        var delayedResize = function (Obj) {
            window.setTimeout(function () {
                setStyle(Obj);
            }, 0);
        };
        if (Obj.addEventListener) {
            Obj.addEventListener('input', function () {
                setStyle(Obj);
            }, false);
            setStyle(Obj);
        } else if (Obj.attachEvent) {
            Obj.attachEvent('onpropertychange', function () {
                setStyle(Obj);
            });
            setStyle(Obj);
        }
        if (window.VBArray && window.addEventListener) { //IE9
            Obj.attachEvent("onkeydown", function () {
                var key = window.event.keyCode;
                if (key == 8 || key == 46) delayedResize(Obj);

            });
            Obj.attachEvent("oncut", function () {
                delayedResize(Obj);
            });
        }
    };
    // 格式转换器
    PetUI.SetJS.onload = () => {
        // 格式转换器
        PetUI.Editor.Converter = new showdown.Converter();
        // 代码高亮插件
        PetUI.Editor.Converter.addExtension({
            type: 'output',
            // 处理接口
            filter: function (text, converter, options) {
                var left = '<pre><code\\b[^>]*>',
                    right = '</code></pre>',
                    flags = 'g',
                    replacement = function (wholeMatch, match, left, right) {
                        // 取消转义
                        match = (match
                            .replace(/&amp;/g, '&')
                            .replace(/&lt;/g, '<')
                            .replace(/&gt;/g, '>')
                        );
                        return left + hljs.highlightAuto(match).value + right;
                    };
                return showdown.helper.replaceRecursiveRegExp(text, replacement, left, right, flags);
            }
        });
        // 公式转换插件
        PetUI.Editor.Converter.addExtension({
            type: 'output',
            filter: function (text, converter, options) {
                var left = '<p>[$]',
                    right = '[$]</p>',
                    flags = 'g',
                    replacement = function (wholeMatch, match, left, right) {
                        // 取消转义
                        match = (match
                            .replace(/&amp;/g, '&')
                            .replace(/&lt;/g, '<')
                            .replace(/&gt;/g, '>')
                            .replace(/(<em>|<\/em>)/g, '_')
                            .replace(/(\\\\|\\)(\r|\n)/g, '\\\\\n')
                        );
                        return katex.renderToString(match);
                    };
                return showdown.helper.replaceRecursiveRegExp(text, replacement, left, right, flags);
            }
        });
    };
})();